<?php
/**
 * Created by PhpStorm.
 * User: wuxiang
 * Date: 2016/9/14
 * Time: 10:43
 */

namespace Common\Libiary;

class LogEX{

    private static $module = 'system';
    private static $level = 'debug';


    /**
     * @param $tag
     * @param array $data
     * @param string $file
     * @param int $line
     */
    public static function SeasLog($tag,array $data,$file=__FILE__,$line=__LINE__){
        $info = [];
        $name = php_sapi_name();
        if($name!='cli'){
            $data['ip'] = get_client_ip();
        }
        $info['business'] = 'operation';
        $info['module'] = static::$module;
        $info['milliseconds'] = round(microtime(true) * 1000);
        $info['tags'] = $tag;
        $info['file'] = $file;
        $info['line'] = $line;
        $info['ssid'] = isset($data['ssid']) ? $data['ssid'] : '';
        $info['user_id'] = isset($data['user_id']) ? $data['user_id'] : 0;
        $info['mobile'] = isset($data['mobile']) ? $data['mobile'] : '';
        $info['data'] = json_encode($data);
        try {
            if (!class_exists('\SeasLog')) throw new \ErrorException('Class \'SeasLog\' not found');
                \SeasLog::log(static::$level, json_encode($info), [], $info['business']);
        }catch (\Exception $e) {
            \Think\Log::write($tag.json_encode($info).PHP_EOL , FILE_APPEND);

        }

    }

    /**
     * @param string $tag
     * @param array $data
     * @param string $file
     * @param int $line
     */
    public static function i($tag, array $data, $file = __FILE__, $line = __LINE__)
    {
        static::$level = 'info';
        call_user_func_array('self::SeasLog', func_get_args());
    }

    /**
     * @param string $tag
     * @param array $data
     * @param string $file
     * @param int $line
     */
    public static function d($tag, array $data, $file = __FILE__, $line = __LINE__)
    {
        static::$level = 'debug';
        call_user_func_array('self::SeasLog', func_get_args());
    }

    /**
     * @param string $tag
     * @param array $data
     * @param string $file
     * @param int $line
     */
    public static function e($tag, array $data, $file = __FILE__, $line = __LINE__)
    {
        static::$level = 'error';
        call_user_func_array('self::SeasLog', func_get_args());
    }

    /**
     * @param string $tag
     * @param array $data
     * @param string $file
     * @param int $line
     */
    public static function w($tag, array $data, $file = __FILE__, $line = __LINE__)
    {
        static::$level = 'warning';
        call_user_func_array('self::SeasLog', func_get_args());
    }

}


